#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

//教材存量表
//void stock()

struct booklist{
	char Class[20]; //班级 
	int ID;           //学号 
	char name[20];    //姓名 
	char ISBN[20];    //书号 
	char press[30];   //出版社 
	double price;     //单价 
	char add[50];     //地址 	
	char title[30];   //书名 
	int number;       //数量 
	char phone[30];   //电话 
}; 

int main()
{
	int i = 0;
	booklist book[200];
	FILE *fp;
	fp = fopen("stockbook.txt","r");
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return 0; 
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s",&book[i].ISBN);
		fscanf(fp,"%lf",&book[i].price);
		fscanf(fp,"%d\n",&book[i].number);
		i++;
	}
	int n = i;
	printf("\t*********************************************************\t\n");
	printf("\t*\t\t\t教材存量表\t\t\t*\t\n");
	printf("\t*********************************************************\t\n");
	printf("\t*\t书号\t\t\t单价\t\t数量\t*\t\n");
	printf("\t*********************************************************\t\n");
	for(i=0;i<n;i++)
	{
		printf("\t*********************************************************\t\n");
		printf("\t*\t%s",book[i].ISBN);
		printf("\t%.2lf",book[i].price);
		printf("\t\t%d\t*\t\n",book[i].number);
		printf("\t*********************************************************\t\n");
	}
	system("pause");
	system("cls");
	int x;
	printf("\n\t是否修改存量？");
	printf("\n\t1.修改");
	printf("\n\t2.退出"); 
	printf("\n\t请选择："); 
	scanf("%d",&x);
	if(x == 1)
	{
		system("cls");
		int flag,number;
		char isbn[20];
		printf("\n\t请输入书号：");
		scanf("%s",isbn);
		getchar();
		printf("\n\t请输入新存量：");
		scanf("%d",&number);
		flag = 0;
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,book[i].ISBN) == 0)
			{
				flag = 1;
				book[i].number = number;
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t未找到对应书号\n"); 
		}
		if(flag == 1)
		{
			i = 0;
			fp = fopen("stockbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return 0; 
	        }
	        for(i=0;i<n;i++)
	        {
	        	fprintf(fp,"%s ",book[i].ISBN);
	        	fprintf(fp,"%lf ",book[i].price);
		        fprintf(fp,"%d\n",book[i].number);
		    }
	        fclose(fp);
			printf("\n\t修改成功\n");
		}
		system("pause");
	}
	if(x == 2)
	{
		return 0;
	}
	return 0;
}